##
## Makefile for  in ~/Dropbox/Projets/raytracer-2016/
##
## Made by Jonathan
## Login   <ledey_l@epitech.net>
##
## Started on  Fri Sep 20 11:58:30 2013 Jonathan
## Last update Sat Sep 21 13:35:36 2013 Jonathan
##

NAME 				= Raytracer

ID				= RAYTRACER::

BIN_DIR				= Binaries

EXE_NAME			= $(BIN_DIR)/$(NAME)

LOG_DIR				= Logs

SRC_DIR				= Sources

OBJ_DIR				= Objects

DEP_DIR				= Dependencies

INC_DIR				= Includes

LIB_DIR				= Libraries

SRC_EXTENSION			= cpp

OBJ_EXTENSION			= opp

DEP_EXTENSION			= dpp

SRCS 				= $(shell find $(SRC_DIR) -name "*.$(SRC_EXTENSION)")

OBJS				= $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(SRCS:.$(SRC_EXTENSION)=.$(OBJ_EXTENSION)))

DEPS				= $(patsubst $(OBJ_DIR)/%, $(DEP_DIR)/%, $(OBJS:.$(OBJ_EXTENSION)=.$(DEP_EXTENSION)))

CXX 				= g++

CXXFLAGS			= -W -Wall -Wextra -Werror -pipe
CXXFLAGS			+= -I $(INC_DIR) -I $(SRC_DIR)
CXXFLAGS			+= -D _GNU_SOURCE

OPTIMIZATIONS_FLAGS		= -O3 -march=native # -s

DEBUG_FLAGS			= -g3 -ggdb3
DEBUG_FLAGS			+= -D DEBUG=''

PROF_FLAGS			+= $(OPTIMIZATIONS_FLAGS) -p

ifeq				($(DEBUG), yes)
CXXFLAGS			+= $(DEBUG_FLAGS)
else ifeq			($(PROF), yes)
CXXFLAGS			+= $(PROF_FLAGS)
else
CXXFLAGS			+= $(OPTIMIZATIONS_FLAGS)
endif

LDFLAGS				+= -lpthread -lsfml-graphics -lsfml-window -lsfml-system

RM				= rm -rf

MKDIR				= mkdir -p

ECHO				= echo -e '[\033[32mOK\033[0m]'

$(EXE_NAME):			$(OBJS)
				@$(MKDIR) $(BIN_DIR)
				@$(MKDIR) $(LOG_DIR)
				@$(CXX) $(OBJS) $(CXXFLAGS) $(LDFLAGS) -o $(EXE_NAME)
				@$(ECHO) '[\033[36m$(ID)$(BIN_DIR)/$(NAME)\033[0m] linked'

all:				$(EXE_NAME)

-include			$(DEPS)

clean:
				@$(RM) $(OBJ_DIR)
				@$(ECHO) '[\033[31m$(ID)Objects\033[0m] cleaned'
				@$(RM) $(DEP_DIR)
				@$(ECHO) '[\033[31m$(ID)Dependencies\033[0m] cleaned'
				@$(RM) $(LOG_DIR)
				@$(ECHO) '[\033[31m$(ID)Logs\033[0m] cleaned'

fclean: 			clean
				@$(RM) $(BIN_DIR)
				@$(ECHO) '[\033[31m$(ID)Executable\033[0m] cleaned'

re:				fclean all

run:				all
				@./$(BIN_DIR)/$(NAME) --no-fullscreen --novid --width=800 --height=600

.PHONY:				clean fclean re all run

$(OBJ_DIR)/%.$(OBJ_EXTENSION):	$(SRC_DIR)/%.$(SRC_EXTENSION)
				@$(eval DIRECTORY := $(shell dirname $* | sed 's/$\\.//'))
				@$(MKDIR) $(OBJ_DIR)/$(DIRECTORY)
				@$(MKDIR) $(DEP_DIR)/$(DIRECTORY)
				$(eval DEPENDENCY := $(patsubst $(OBJ_DIR)/%, $(DEP_DIR)/%, $(@:.$(OBJ_EXTENSION)=.$(DEP_EXTENSION))))
				@$(CXX) -MD -MF $(DEPENDENCY) -c -o $@ $< $(CXXFLAGS)
				@$(ECHO) '[\033[33m$(ID)$<\033[0m] object builded (\033[34m$(ID)$(OBJ_DIR)/$*.$(OBJ_EXTENSION)\033[0m)'
				@$(ECHO) '[\033[33m$(ID)$<\033[0m] dependency builded (\033[35m$(ID)$(DEP_DIR)/$*.$(DEP_EXTENSION)\033[0m)'
